package com.zmx.patients.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.ExpertTeamAdapter;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.Doctor;
import com.zmx.patients.pojo.ExpertTeam;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 程序猿：胖胖祥
 * 时 间：2020/11/17  22:52
 * 功 能：专家团队
 */
public class ExpertTeamActivity extends BaseActivity {


    @BindView(R.id.reView)
    RecyclerView reView;
    @BindView(R.id.doctor_head)
    CircleImageView doctorHead;
    @BindView(R.id.doctor_name)
    TextView doctorName;
    @BindView(R.id.doctor_title)
    TextView doctorTitle;
    @BindView(R.id.doctor_department)
    TextView doctorDepartment;
    @BindView(R.id.article_collects)
    TextView article_collects;
    @BindView(R.id.no_data)
    TextView no_data;
    @BindView(R.id.doctor_button)
    Button doctorButton;

    private String did,name,class_name,head,title;

    private Doctor doctor = null;
    private ExpertTeamAdapter adapter;
    private List<ExpertTeam> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_expert_team;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        did = this.getIntent().getStringExtra("did");
        head = this.getIntent().getStringExtra("head");
        class_name = this.getIntent().getStringExtra("class_name");
        name = this.getIntent().getStringExtra("name");
        title = this.getIntent().getStringExtra("title");
        doctor = (Doctor) this.getIntent().getSerializableExtra("doctor");

        Glide.with(mActivity).load(head).into(doctorHead);
        doctorName.setText(name);
        doctorDepartment.setText(class_name);
        doctorTitle.setText(title);
        article_collects.setText(name+"专家团队列表");

        adapter = new ExpertTeamAdapter(mActivity,handler);
        lists = new ArrayList<>();
        reView.setLayoutManager(new LinearLayoutManager(mActivity));
        reView.setAdapter(adapter);

        getData();

    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Gson gson = new Gson();
                        JSONArray data = object.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {

                            ExpertTeam et = gson.fromJson(data.get(i).toString(), ExpertTeam.class);
                            lists.add(et);
                        }
                        if (lists.size()>0){

                            no_data.setVisibility(View.GONE);

                        }else {

                            no_data.setVisibility(View.VISIBLE);
                        }
                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 2:

                    Intent intent = new Intent();
                    intent.setClass(mActivity, ExpertTeamDetailActivity.class);
                    intent.putExtra("team_id",""+msg.obj.toString());
                    intent.putExtra("doctor", doctor);
                    mActivity.startActivity(intent);

                    break;

            }

        }
    };

    public void getData() {

        List<String> ls = new ArrayList<>();
        ls.add(did);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "teamList");
        params.put("data[user_id]", did);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.team/entry", params, handler, 1, 404);

    }


    @OnClick(R.id.doctor_button)
    public void onViewClicked() {
    }
}